<template lang="jade">
form-page(:url.sync="postBank.url"  , :rdata.sync="postBank.data")
com-alert(:show.sync="alertData.show", :title.sync="alertData.title", :buttontxt.sync="alertData.buttontxt", :content.sync="alertData.content")
div.repd
	common-dialog(v-show="showDialog", @update:setparent=" setparent ", :close="true")
		div.redpack
			div.title 可用红包
			div.note 系统会自动为您选择激活红包最大收益的匹配方案，若您想激活其余红包，则需要增加相应的投资金额。
			div.con
				coupon(:items="redlist")

			ul.ux_row(style="padding:5px 10px;font-size:14px;")
				li.ux_flex
					投资金额:
					b.tred {{money}}元
				li.ux_flex
					激活红包:
					b.tred {{redPackData.reward}}元

			ul.ux_row
				li.ux_flex.tcenter
					input.p_btn_nob(type="button", value="充值", @click="goR()")
				li.ux_flex.tcenter
					button(v-bind:class="subBtn.class", v-bind:disabled="subBtn.disabled",   @click="goTender()") {{subBtn.txt}}

div
	div.return-bar(@click="back()")
		i.iconfont &#xe668;
		{{header_txt}}




	div.detail-page
		loading(:show="show_loading", text="正在加载")
		section.form
			div
				div.postForm
					input.money(name="money",v-model="money",placeholder="请输入100的整数倍数，最低100元",autocomplete="off")
					input(type="hidden",name="borrow_nid",v-model="detailId")
					span.rmb 元
					ul.cbox
						li.balance
							span 可用余额￥
								b {{banlance}}
								元
						li.btn
							button(v-bind:class="postBtn.class", v-bind:disabled="postBtn.disabled",  type="button",  @click="onSubmit()") {{postBtn.txt}}

	div.detail-info.bottom_space

		div(v-if="typeId == 1")
			header 借款人信息
			article.content(v-if=" items.company  ")
				div 借款公司：{{items.company.company_name}}
				div 注册地址：{{items.company.company_address}}
				div 工商登记号码：{{items.company.company_license}}
				div 登记状态：{{items.company.company_reg_status}}
				div 所属行业：{{items.company.company_industry}}
				div 借款用途：{{items.borrow_use}}



			article.content(v-else)
				div 姓名：{{items.name}}
				div 性别：{{items.sex}}
				div 借款帐号：{{items.phone}}

				div 身份证：{{items.idcard}}
				div 年龄：{{items.age}}
				div 婚姻状况：{{items.marriage}}
				div 户籍：{{items.census_register}}
				div 月收入：{{items.income}}
				div 职位：{{items.position}}
				div 资产状况：{{items.asset}}
				div 现居地：{{items.residence}}
				div 借款用途：{{items.borrow_use}}

			header 风控审核栏目(已审核文件可在还款保障页面查看)

			article.content(v-if="items.borrow_name_no_type == 1 ")
				div 房产证：{{items.register}}
				div 二代身份证：{{items.idcard_check}}
				div 国土局通知书：{{items.driving_license}}
				div 抵押登记现场图：{{items.insurance_policy}}
				div 借款合同：{{items.loan_contract}}
				div 抵押合同：{{items.mortgage_contract}}
				div 公证书：{{items.confirmation}}
				div 委托书：{{items.debt_buyback}}
				div 资料状态：{{items.data_status}}
				div 借款风险度：{{items.grade}}

			article.content
				div GPS：{{items.gps}}
				div 抵押登记：{{items.register}}
				div 二代身份证：{{items.idcard_check}}
				div 行驶证：{{items.driving_license}}
				div 机动车登记证：{{items.vehicle}}
				div 保险单：{{items.insurance_policy}}
				div 评估表：{{items.evaluation_form}}
				div 审核表：{{items.audit_form}}
				div 借款合同：{{items.loan_contract}}
				div 抵押合同：{{items.mortgage_contract}}
				div 收款确认书：{{items.confirmation}}
				div 债权回购书：{{items.debt_buyback}}
				div 车贷品种：{{items.varieties}}
				div 资料状态：{{items.data_status}}
				div 借款风险度：{{items.grade}}


			header 借款描述
			article.content {{items.borrow_describe}}

			header 借款记录
			article.content
				div 成功借款次数：{{items.success}}
				div 成功还款次数：{{items.recover_num}}
				div 逾期次数：0
				div 预期总金额：0
				div 逾期己还清次数：0
				div 预期时间：0

			header 回款后收取利息管理费(利息*10%)


		section.content.list(v-if="typeId == 3")
			ul
				li
					div.title.cbox
						span.cl 投标人({{people}}人)
						span.cl 投标金额
						span.cl.sp 投标时间
						span.cl 投资方式


				li(v-for=" (index, item) in invest_record")
					div.cbox
						span.cl.txt {{ item.username }}
						span.cl.txt ￥{{ item.account }}
						span.cl.txt.sp {{ item.addtime }}
						span.cl.txt {{item.contents}}


		section.content.list(v-if="typeId == 4")
			ul
				li
					div.title.cbox
						span.cl 期限
						span.cl 还款日期
						span.cl 状态
						span.cl 应还本息

				li(v-for=" (index, item) in return_record")
					div.cbox
						span.cl.txt {{item.repay_period}}/{{return_record.length}}
						span.cl.txt {{item.repay_time}}
						span.cl.txt {{item.repay_type}}
						span.cl.txt {{item.repay_account}}元



</template>

<script>
import '../../assets/sass/invest/invest.scss'

import {c_borrow_detail, Api_Tender, c_order_detail_loan, c_order_detail_investrecord, c_order_detail_returnrecord} from '../../config/api'
import {Loading} from 'vux'
import FormPage from '../../components/common/form'
import ComAlert from '../../components/common/alert'
import api from '../../data/api';


import { mapGetters, mapActions } from 'vuex'
import CommonDialog from "../../components/dialog";  //弹窗插件
import Coupon from "../../components/coupon";
import investOjb from '../../controller/invest';

export default {
	data () {
		return {

			subBtn:{txt:'确认投标',class:'p_btn', disabled:false},
			postBtn:{txt:'立即投标', class:'', disabled:false},
			postForm:{},
			postBank:{
				url:'',
				data:{
					RequestData:'',
					transCode:''
				}
			},
			redlist: [],


            alertData:{
                show:false,
                title:'标题',
                buttontxt:'确定',
                content:'sign'
            },

			people:'',

			show_loading:false,
			busy:false,
			banlance: 0,
			money:'',
			borrow_nid: '',
			typeId: '0',
			header_txt:"",
			content: '客户急需一笔资金用于生意周转。',
			items: [],
			invest_record:[],
			return_record:[]
		}
	},
	components: {
		Coupon,
		CommonDialog,
		ComAlert,
		FormPage,
		Loading
	},

    computed: {
      ...mapGetters([
		  'showDialog', 'redPackData'
      ])
	},

	methods: {

		isInteger(obj) {
			return Math.floor(obj) == obj
		},

		verMoney(){
			if(this.money == '') return 0;
			if(this.money == undefined) return 0;
			if(this.money == null) return 0;
			if(this.isInteger(this.money) == false) return 0;
			this.money = parseInt(this.money);
			if(this.money < 100) return 1;
			return 2;
		},

		goR(){
			this.$route.router.go('/recharge_guide');
		},

		setparent(params){
			this.$store.dispatch('setShowDialog', params.status)
		},

		back(){
			//this.$route.router.go('/auto-buy')
			window.history.back();
		},


		process(key, close){

			if(close){
				this.$store.dispatch('setShowLoading', true), this.busy = true;
			}else{
				this.$store.dispatch('setShowLoading', false), this.busy = false;
			}

			switch (key) {
				case 1:
					if(close){
						this.postBtn.txt = '正在处理', this.postBtn.class = 'grey_btn', this.postBtn.disabled = true;
					}else{
						this.postBtn.txt = '立即投标', this.postBtn.class = '', this.postBtn.disabled = false;
					}
					break;
				case 2:
					if(close){
						this.subBtn.txt = '正在处理', this.subBtn.class = 'grey_btn', this.subBtn.disabled = true;
					}else{
						this.subBtn.txt = '确认投标', this.subBtn.class = 'p_btn', this.subBtn.disabled = false;
					}
					break;
			}
		},

		async goTender(){


			var vm = this;
			if (this.busy) return;

			var am = parseInt(this.money), bm = parseInt(this.redPackData.tender);

			if( am < bm ){
				this.alertData = { show: true, title: '信息提示', buttontxt: '确定', content: '投标金额小于活动条件金额,请重新选择红包!' };
				return;
			}

			var data = {'borrow_nid': this.borrow_nid, 'money': this.money, 'couponArr':this.redPackData.id, 'ajax': '1'}
			this.process(2, true);
			var res = await investOjb.tender(data); //直接投标
			if(res.status == false) this.process(2, false);
		},

		async onSubmit() {
			var vm = this;
			if(this.busy) return;

			var rnum = this.verMoney();
			if(rnum == 0){
				this.alertData = { content: '金额输入有问题,请重新输入!', show: true, title: '信息提示', buttontxt: '确定'};
				return;
			}

			if(rnum == 1){
				this.alertData = { content: '金额请输入大于等于100元以上, 谢谢!', show: true, title: '信息提示', buttontxt: '确定'};
				return;
			}

			var data = {'borrow_nid': this.borrow_nid, 'money': this.money, 'ajax': '1'}
			this.$store.dispatch('setShowLoading', true), this.busy = true;
			this.process(1, true);
			/* var res = await investOjb.getRedPack(data);
			console.log(res)
			if(res.status == false){
				res = await investOjb.tender(data);
				if(res.status == false) this.process(1, false);
			}else{
				this.process(1, false);
			} */

			var res = await investOjb.tender(data);
			if(res.status == false) this.process(1, false);
		},

		return(next){
			var that = this;
			this.$http({url: c_order_detail_returnrecord+'&borrow_nid='+that.borrow_nid, method: 'POST'}).then(function (response) {
				that.return_record = response.data.data;
				next();
			});
		},

		invest(next){
			var that = this;
			this.$http({url: c_order_detail_investrecord+'&type=tender&borrow_nid='+that.borrow_nid, method: 'POST'}).then(function (response) {
				that.invest_record = response.data.data;
				that.people = response.data.people;
				next();
			});
		},

		//标的详情 - 还款信息
		async loan_repay_info(next){
			var setType;
			var that = this;
			/* this.$http({url: c_order_detail_loan+'&borrow_nid='+that.borrow_nid, method: 'POST'}).then(function (response) {
					that.items = response.data.data
					next()
				}, function (response) {
						console.log(response)
				}); */

			var res = await api.invest.loan_repay_info({'borrow_nid':that.borrow_nid})
			that.items = res.data;
			next();

		},


		async loan_info() {
			var that = this, url = "";
			//if(this.typeId=='1')
				//url = '&name_no='+this.detailId;
			//else
			//url = '&borrow_nid='+this.borrow_nid;
			this.$store.dispatch('setShowLoading', true);

			/* this.$http({url: c_borrow_detail+url, method: 'get'}).then(function (response) {
				this.$store.dispatch('setShowLoading', false);
				that.item = response.data.data
				that.banlance = response.data.banlance
				document.title = that.item.name + " - 有喜财富投标理财"

			}, function (response) {
					console.log(response)
			}); */

			var res = await api.invest.loan_detail({'borrow_nid':that.borrow_nid})
			this.$store.dispatch('setShowLoading', false);
			if(res.data){
				that.item = res.data
				that.banlance = res.banlance
				document.title = that.item.name + " - 有喜财富投标理财"
			}
		}

	},
	route: {
		activate ( transition ) {
			investOjb.init({eventBus:this});
			this.typeId = transition.to.params.typeId
			this.borrow_nid = transition.to.query.borrow_nid

			this.$store.dispatch('setShowDialog', false);

			if(this.typeId=="1"){
				this.header_txt = "借款详情"
				this.loan_repay_info( function() {
					transition.next();
				})
			}

			if(this.typeId=="3"){
				this.header_txt = "投标记录"
				this.invest( function() {
					transition.next();
				})
			}

			if(this.typeId=="4"){
				this.header_txt = "还款计划"
				this.return( function() {
					transition.next();
				})
			}


			this.loan_info()

		}
	}
}
</script>



<style lang="scss" scoped>
.grey_btn{
	background-color:#999!important;
}
</style>


<style lang="scss">
.repd{
	.dialog.active{
		width:95%!important;
	}

	.redpack{
		background-color:#fff;


		.title{
			font-size:16px;
			padding:5px 10px;
		}

		.note{
			font-size:12px;
			margin-bottom:10px;
			padding:0px 10px;
		}
		.con{
			background-color:#f4f8fb;
			padding:10px;
			padding-bottom:1px;
			height: 200px;
    		overflow-y: scroll;
		}

		.ux_flex{
			padding:5px 0px;
		}
		input, button{
			width:80%;
			padding:8px 0px;
			font-size:14px;
			&.p_btn{
				border:none;
			}
			&.grey_btn{
				color:#fff;
				border:none;
				border-radius: .1rem;
				background-color:#999!important;
			}

		}

		.coupon li .tag span{
			font-size:14px;
		}
		.coupon li .tag strong{
			    font-size: 16px;
		}
		.coupon li .info{
			width:56%;
		}
	}
}
</style>
